package com.gking.processHarvest.service.Tag;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.processHarvest.common.BaseContext;
import com.gking.processHarvest.common.CustomException;
import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.entity.HarvestTags;
import com.gking.processHarvest.entity.Tag;
import com.gking.processHarvest.mapper.TagMapper;
import com.gking.processHarvest.service.HarvestTags.HarvestTagsService;
import com.gking.processHarvest.service.Tag.TagService;
import com.gking.processHarvest.utils.CheckStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)//三级缓存解决bean循环注入问题
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private HarvestTagsService harvestTagsService;


    /**
     * 创建标签
     *
     * @param tagList 标签列表
     * @return s
     */
    @Override
    public Res<String> create(List<Tag> tagList) {
        if (tagList.size() <= 0) return Res.error("请将参数补充完整");
        tagList.forEach((item) -> {
            if (item.getName() == null || item.getColor() == null || item.getName().equals("") || item.getColor().equals(""))
                throw new CustomException("创建的标签信息残缺！");
        });

        //对传入的标签进行查重
        Set<Tag> setTags = new HashSet<>(tagList);
        if (setTags.size() != tagList.size()) return Res.error("创建失败，传入的标签存在重复标签！");

        //对已有的标签进行查重
        tagList.forEach((item) -> {
            CheckStatus<Tag> checkName = new CheckStatus<>();
            LambdaQueryWrapper<Tag> wrapper = checkName.checkDuplicateName(
                    item.getName(), Tag::getName,
                    Tag::getMasterId, Tag::getIsDelete);
            if (this.count(wrapper) > 0) throw new CustomException("标签 " + item.getName() + " 已存在！");
        });

        this.saveBatch(tagList);

        return Res.success("创建成功！");
    }


    /**
     * 修改标签信息
     *
     * @param id 标签id
     * @param name 标签名称
     * @param color 颜色
     * @return s
     */
    @Override
    @Transactional
    public Res<String> update(String id, String name, String color) {
        if (id == null || name == null || color == null) return Res.error("请将参数补充完整");

        //查看标签是否存在且未删除
        CheckStatus<Tag> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Tag> lambdaQueryWrapper = checkStatus.checkIsDelete(
                id, Tag::getId,
                Tag::getMasterId, Tag::getIsDelete);
        if (this.getOne(lambdaQueryWrapper) == null) return Res.error("此标签已删除，无法修改！");

        //对修改的标签名进行查重
        new LambdaQueryWrapper<>();
        CheckStatus<Tag> checkName = new CheckStatus<>();
        LambdaQueryWrapper<Tag> queryWrapper = checkName.checkDuplicateName(
                name, Tag::getName,
                Tag::getMasterId, Tag::getIsDelete);
        if (this.getOne(queryWrapper) != null) return Res.error("此标签已存在，换一个标签名！");

        //无重复则进行修改
        LambdaUpdateWrapper<Tag> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Tag::getId, id);
        updateWrapper.eq(Tag::getMasterId, BaseContext.getCurrentId());
        updateWrapper.set(Tag::getName, name);
        updateWrapper.set(Tag::getColor, color);
        this.update(updateWrapper);

        //修改收获标签列表里的信息
        LambdaUpdateWrapper<HarvestTags> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(HarvestTags::getTagId, id);
        wrapper.eq(HarvestTags::getMasterId, BaseContext.getCurrentId());
        wrapper.set(HarvestTags::getName, name);
        wrapper.set(HarvestTags::getColor, color);
        harvestTagsService.update(wrapper);

        return Res.success("修改标签信息成功！");
    }


    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @Override
    public Res<String> delete(String id) {
        if (id == null || id.equals("")) return Res.error("请将参数补充完整");

        //是否存在且未删除
        CheckStatus<Tag> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Tag> lambdaQueryWrapper = checkStatus.checkIsDelete(
                id, Tag::getId,
                Tag::getMasterId, Tag::getIsDelete);
        if (this.getOne(lambdaQueryWrapper) == null) return Res.error("删除失败！");

        //存在且未删除则删除
        LambdaUpdateWrapper<Tag> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Tag::getMasterId, BaseContext.getCurrentId());
        updateWrapper.eq(Tag::getId, id);
        updateWrapper.set(Tag::getIsDelete, 1);
        this.update(updateWrapper);

        //修改收获标签列表里的信息
        LambdaUpdateWrapper<HarvestTags> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(HarvestTags::getMasterId, BaseContext.getCurrentId());
        wrapper.eq(HarvestTags::getTagId, id);
        harvestTagsService.remove(wrapper);

        return Res.success("删除标签成功！");
    }


}
